package com.joke;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import com.guohead.sdk.GuoheAdLayout;
import com.guohead.sdk.GuoheAdManager;
import com.joke.adapter.JokeAdapter;
import com.joke.model.Constants;
import com.joke.model.Story;

public class JokeList extends Activity{

	private List<Story> mList = new ArrayList<Story>();
	private ListView jokelist;
	private JokeAdapter adapter;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.joke);
		
		/**guohe ad start**/
		LinearLayout.LayoutParams GuoheAdLayoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        LinearLayout footer = (LinearLayout)findViewById(R.id.adLayout);
        GuoheAdLayout adLayout = new GuoheAdLayout(JokeList.this);
        footer.addView(adLayout, GuoheAdLayoutParams);
        footer.invalidate();
        /**guohe ad end**/
         
        mList = Constants.gStoryList;

		adapter = new JokeAdapter(this,mList);
		jokelist = (ListView)this.findViewById(R.id.jokelist);
		jokelist.setAdapter(adapter);
		
		jokelist.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent();
				intent.putExtra("jokeID",position);
				intent.setClass(JokeList.this, JokeText.class);
				JokeList.this.startActivity(intent);
			}
		});
	}
    
	
	@Override
    protected void onDestroy() {
      super.onDestroy();
      GuoheAdManager.finish(this);
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1, 0, JokeList.this.getString(R.string.update));
		menu.add(0, 0, 0, JokeList.this.getString(R.string.feedBack));
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
			case 0:
				/**发送邮件**/
			    Intent email = new Intent(android.content.Intent.ACTION_SEND);
			    email.setType("plain/text");
			    String[] emailReciver = new String[]{getString(R.string.feedBackEmailHu), getString(R.string.feedBackEmailQiu)};
			    String emailSubject = getString(R.string.feedBackSubject);
			    String emailBody = "";
			    /**设置邮件默认地址**/
			    email.putExtra(android.content.Intent.EXTRA_EMAIL, emailReciver);
			    /**设置邮件默认标题**/
			    email.putExtra(android.content.Intent.EXTRA_SUBJECT, emailSubject);
			    /**设置要默认发送的内容**/
			    email.putExtra(android.content.Intent.EXTRA_TEXT, emailBody);
			    /**调用系统的邮件系统**/
			    startActivity(Intent.createChooser(email, getString(R.string.feedBackTips))); 
				return true;
			case 1:
				/**更新列表**/
				int reCode = Constants.update();
				switch(reCode){
					/**获取列表成功**/
					case 200:
						mList = Constants.gStoryList;
						adapter.setItemList(mList);
						adapter.notifyDataSetChanged();
						break;
					/**不能连接服务器**/
					case 1:
						Toast.makeText(JokeList.this, getString(R.string.disconnect), Toast.LENGTH_SHORT).show();
						break;
					/**获取列表异常**/
					case -1:
						Toast.makeText(JokeList.this, getString(R.string.disconnect), Toast.LENGTH_SHORT).show();
						break;
					default:
						break;
				}
				
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}
}
